import wxBaaS from '../sdk-wechat.3.23.5';

// 获取药膳分类列表
export async function getMedifoodClassifications() {
  try {
    const MedifoodCaseClassification = new wxBaaS.TableObject('Medifood_case_classification'); 
    const query = new wxBaaS.Query();
    const response = await MedifoodCaseClassification.setQuery(query).find();
    return response.data.objects;
  } catch (error) {
    console.error('获取药膳分类列表失败:', error);
    throw error;
  }
}

// 添加药膳分类
export async function addMedifoodClassification(categoryData) {
  try {
    const MedifoodCaseClassification = new wxBaaS.TableObject('Medifood_case_classification');
    const classification = MedifoodCaseClassification.create();
    Object.keys(categoryData).forEach(key => classification.set(key, categoryData[key]));
    const response = await classification.save();
    return response.data.id;
  } catch (error) {
    console.error('添加药膳分类失败:', error);
    throw error;
  }
}

// 更新药膳分类
export async function updateMedifoodClassification(categoryId, updateData) {
  try {
    const MedifoodCaseClassification = new wxBaaS.TableObject('Medifood_case_classification');
    const classification = MedifoodCaseClassification.getWithoutData(categoryId);
    Object.keys(updateData).forEach(key => classification.set(key, updateData[key]));
    await classification.update();
    return true;
  } catch (error) {
    console.error('更新药膳分类失败:', error);
    throw error;
  }
}

// 删除药膳分类
export async function deleteMedifoodClassification(categoryId) {
  try {
    const MedifoodCaseClassification = new wxBaaS.TableObject('Medifood_case_classification');
    const classification = MedifoodCaseClassification.getWithoutData(categoryId);
    await classification.delete();
    return true;
  } catch (error) {
    console.error('删除药膳分类失败:', error);
    throw error;
  }
}